<?php	
	/*
		this file constructs the appropriate JSON formatted string to be downloaded by the user so that the
		diagrams can be opened again
	*/
	require_once("../functions.php");
	
	if(!isset($_GET["id"])){
		die("No parameters");
	}
	
	connectToDB();
	
	$sql1 = "select * from project where id=$_GET[id]";
	$sql2 = "select d.id, d.name,d.jsonId,d.diagramType,d.width,d.height,d.description,d.sortOrder,d.custom from diagram d, project p where d.project_id = p.id and p.id = $_GET[id]";
	$sql3 = "select c.id, c.name,c.jsonId,c.componentType,c.x,c.y,c.sortOrder,c.label,c.description,c.custom from component c, diagram d, project p where c.diagram_id=d.id and d.project_id=p.id and p.id=$_GET[id]";
	$sql4 = "select distinct cn.jsonId,cn.name,cn.component1_dir,cn.component2_dir,cn.connectionType,cn.cardinality1,cn.cardinality2,cn.component1_jsonId,cn.component2_jsonId,cn.stereotype,cn.description,cn.custom from connection cn, component c, diagram d, project p where (cn.component1_id=c.id or cn.component2_id=c.id) and (c.diagram_id=d.id and d.project_id=p.id and p.id=$_GET[id])";
	$sql5 = $sql = "SELECT ch.id,ch.jsonId,ch.parentType,ch.childType,ch.modifier,ch.type,ch.name,ch.parameters,ch.component_id FROM children ch, component c, diagram d, project p WHERE p.id=$_GET[id] AND p.id=d.project_id AND d.id = c.diagram_id AND c.id = ch.component_id";
	
	$projResult = mysql_query($sql1) or die("<br />Project: " . mysql_error());
	if(mysql_num_rows($projResult)!=1){
		die("No project found.");
	}
	
	$diag = mysql_query($sql2) or die("<br />Diagram: " . mysql_error());
	$comp = mysql_query($sql3) or die("<br />Component: " . mysql_error());
	$conn = mysql_query($sql4) or die("<br />Connection: " . mysql_error());
	$chil = mysql_query($sql5) or die("<br />Children: " . mysql_error());
	
	$proj = mysql_fetch_array($projResult);
	$numDiag = mysql_num_rows($diag);
?>
{
	"identifier": "id", 
	"label": "name", 
	"items": [
		{
			"id": "project", 
			"name": "<?php echo $proj["name"] ?>", 
			"diag_counter": <?php echo $proj["diagramCounter"] ?>, 
			"comp_children_counter": <?php echo $proj["componentChildrenCounter"] ?>, 
			"comp_counter": <?php echo $proj["componentCounter"] ?>, 
			"conn_counter": <?php echo $proj["connectionCounter"] ?>, 
			"dateCreated": "<?php echo $proj["dateCreated"] ?>", 
			"dateModified": "<?php echo $proj["dateModified"] ?>", 
			"db_key": <?php echo $proj["id"] ?>, 
			"children": [
				<?php
					for($i=0; $i<$numDiag; $i++){
						$currDiag = mysql_fetch_array($diag);
						echo "{";
							echo '"id": "' . $currDiag["jsonId"] . '",';
							echo '"name": "' . $currDiag["name"] . '",';
							echo '"uwstype": "diagram", ';
							echo '"umltype": "' . $currDiag["diagramType"] . '", ';
							echo '"width": ' . $currDiag["width"] . ', ';
							echo '"height": ' . $currDiag["height"] . ', ';
							echo '"sort_order": ' . $currDiag["sortOrder"] . ', ';
							echo '"custom": "' . $currDiag["custom"] . '", ';
							echo '"description": "' . $currDiag["description"] . '", ';
							echo '"children": [';
								
								$sql_comp = $sql3 . " and d.id= " . $currDiag["id"];
								$result = mysql_query($sql_comp) or die("Select comp by diag: " . mysql_error());
								$numComp = mysql_num_rows($result);
								
								for($j=0;$j<$numComp;$j++){
									$currComp = mysql_fetch_array($result);
									echo '{';
										echo '"id": "'.$currComp["jsonId"].'", ';
										echo '"name": "'.$currComp["name"].'", ';
										echo '"uwstype": "component", ';
										echo '"umltype": "'.$currComp["componentType"].'", ';
										echo '"x": '.$currComp["x"].', ';
										echo '"y": '.$currComp["y"].', ';
										echo '"sortOrder": '.$currComp["sortOrder"].', ';
										echo '"label": "'.$currComp["label"].'", ';
										echo '"description": "'.$currComp["description"].'", ';
										echo '"comp_children": [';
											
											$sql_chil = $sql5 . " AND c.id=" . $currComp["id"];
											$resultChil = mysql_query($sql_chil) or die("Select child by comp: " . mysql_error());
											$numChil = mysql_num_rows($resultChil);
											
											for($k=0; $k<$numChil; $k++){
												$currChil = mysql_fetch_array($resultChil);
												echo '{"id": "' . $currChil["jsonId"] . '", ';
												echo '"type": "' . $currChil["type"] . '", ';
												echo '"parentType": "' . $currChil["parentType"] . '", ';
												echo '"childType": "' . $currChil["childType"] . '", ';
												echo '"modifier": "' . $currChil["modifier"] . '", ';
												echo '"name": "' . $currChil["name"] . '", ';
												echo '"parameters": "' . $currChil["parameters"] . '"} ';
												if($k!=$numChil-1) echo ',';
											}
											mysql_free_result($resultChil);
											
										echo '],';
										echo '"custom": "'.$currComp["custom"].'"';
									echo '}'; 
									
									if($j!=$numComp-1) echo ',';
								}
								mysql_free_result($result);
							echo "],";
							echo '"connections": [';
								
								$sql_conn = $sql4 . " and d.id= " . $currDiag["id"];
								$result = mysql_query($sql_conn) or die("Select conn by diag: " . mysql_error());
								$numConn = mysql_num_rows($result);
								
								for($j=0;$j<$numConn;$j++){
									$currConn = mysql_fetch_array($result);
									
									echo '{';
										echo '"id": "'.$currConn["jsonId"].'", ';
										echo '"name": "'.$currConn["name"].'", ';
										echo '"uwstype": "connection", ';
										echo '"umltype": "'.$currConn["connectionType"].'", ';
										echo '"ref1_id": "'.$currConn["component1_jsonId"].'", ';
										echo '"ref1_dir": "'.$currConn["component1_dir"].'", ';
										echo '"ref2_id": "'.$currConn["component2_jsonId"].'", ';
										echo '"ref2_dir": "'.$currConn["component2_dir"].'", ';
										echo '"cardinality1": "'.$currConn["cardinality1"].'", ';
										echo '"cardinality2": "'.$currConn["cardinality2"].'", ';
										echo '"stereotype": "'.$currConn["stereotype"].'", ';
										echo '"description": "'.$currConn["description"].'", ';
										echo '"custom": "'.$currConn["custom"].'"';
									echo '}';
									
									if($i!=$numConn-1) echo ",";
								}
								mysql_free_result($result);
							echo ']';
						echo "}";
						
						if($i!=$numDiag-1) echo ",";
					}
				?>
			]
		}
	]
}
<?php
	mysql_free_result($projResult);
	mysql_free_result($diag);
	mysql_free_result($comp);
	mysql_free_result($conn);
	mysql_free_result($chil);
?>